package com.matrix.draft.juc.day5;

import java.util.concurrent.locks.ReentrantLock;

public class TestSync {

    public static long ticket = 0;

    public static ReentrantLock lock = new ReentrantLock();

    public void calculate(){
        TestSync.lock.lock();
        ++TestSync.ticket;
        TestSync.lock.unlock();
    }

    public static void main(String[] args) throws Exception {
        TestSync instance = new TestSync();
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 5000; i++) {
                instance.calculate();
            }
        });
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 5000; i++) {
                instance.calculate();
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(TestSync.ticket);
    }

}
